package com.suray.rgv.son.comm;

import com.suray.basic.wcs.utils.LoggerType;
import com.suray.basic.wcs.utils.WcsFuncUtil;
import com.suray.rgv.son.data.ModbusWriteMessage;
import com.suray.rgv.son.data.ModbusWriteRecMessage;
import com.suray.rgv.son.data.RgvSonControlBean;
import com.suray.rgv.util.RgvLogger;

import java.io.IOException;
import java.util.List;

public class RgvSonWriteData {

    private ModbusWriteMessage writeMessage;

    private ModbusWriteRecMessage writeRecMessage;

    private RgvSonComm rgvSonComm;

    public RgvSonWriteData(RgvSonComm rgvSonComm) {
        this.rgvSonComm = rgvSonComm;
        writeMessage = new ModbusWriteMessage();
        writeRecMessage = new ModbusWriteRecMessage();
    }

    public void sendData(int startAddress, int data[], int count) throws IOException {
        try {
            ModbusWriteMessage.ModbusWriteMsg writeMsg = writeMessage.setMessageInfo(startAddress, data);
            byte[] recData = rgvSonComm.comm("写数据" , writeMsg, 5);
            writeRecMessage.readMessage(recData);
            validateRecMessage(writeRecMessage, writeMsg, recData);
        } catch (IOException e) {
            if (count > 0) {
                RgvLogger.exceptionLog(LoggerType.WARN, startAddress, "写入数据失败", e, this.getClass());
                count--;
            } else {
                throw e;
            }
        }
    }

    /**
     * 校验返回数据
     * @param writeRecMessage
     * @param writeMessage
     * @param recData
     */
    private void validateRecMessage(ModbusWriteRecMessage writeRecMessage,
                                    ModbusWriteMessage.ModbusWriteMsg writeMessage, byte[] recData) {
        writeRecMessage.validateMessage(writeMessage.getSlaveAddr(), writeMessage.getStartAddr(),
                writeMessage.getDataLength(), writeRecMessage.calcCrc(recData));
    }

    public void sendData(int startAddress, RgvSonControlBean controlBean, int count) throws IOException {
        sendData(startAddress, changeData(controlBean), count);
    }

    private int[] changeData(RgvSonControlBean controlBean) {
        int[] data = new int[0x6F];

        data[0] = controlBean.getTaskDirection();
        data[1] = controlBean.getNodeNum();
        data[2] = controlBean.getOriginalCodeH();
        data[3] = controlBean.getOriginalCodeL();

        int index = 4;
        for (RgvSonControlBean.NodeInfo nodeInfo : controlBean.getNodeInfo()) {
            data[index++] = nodeInfo.getDistance();
            data[index++] = nodeInfo.getCodeH();
            data[index++] = nodeInfo.getCodeL();
        }

        data[index++] = controlBean.getTaskId();
        data[index++] = controlBean.getStartTask();
        data[index++] = controlBean.getSegment();
        index += 3;
        data[index++] = controlBean.getTaskNodeNum();

        for (RgvSonControlBean.TaskInfo taskInfo : controlBean.getTaskInfo()) {
            data[index++] = WcsFuncUtil.combineToWord(taskInfo.getAction().byteValue(),
                    taskInfo.getLocation().byteValue());
        }

        return data;
    }
}
